<template>
  <div 
    :class="['poker-card', cardSizeClass, suitClass, { 'selected': selected }, { 'card-back': isBack }]" 
    @click="$emit('click')"
  >
    <template v-if="!isBack">
      <div class="card-corner top-left">
        <div class="card-rank">{{ displayRank }}</div>
        <div class="card-suit">{{ suitSymbol }}</div>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import type { PropType } from 'vue';
import type { Card } from '@/types/card';

const props = defineProps({
  card: {
    type: [String, Object] as PropType<string | Card>,
    required: true
  },
  selected: {
    type: Boolean,
    default: false
  },
  isBack: {
    type: Boolean,
    default: false
  },
  size: {
    type: String as PropType<'normal' | 'small' | 'mini'>,
    default: 'normal'
  }
});

defineEmits(['click']);

const suitSymbol = computed(() => {
  if (props.isBack || !props.card) return '';
  if (typeof props.card === 'string') {
    return props.card.charAt(0);
  }
  switch (props.card.suit) {
    case 'HEARTS': return '♥';
    case 'DIAMONDS': return '♦';
    case 'CLUBS': return '♣';
    case 'SPADES': return '♠';
    default: return '';
  }
});

const rank = computed(() => {
  if (props.isBack || !props.card) return '';
  if (typeof props.card === 'string') {
    return props.card.substring(1);
  }
  return props.card.rank;
});

const displayRank = computed(() => {
  const rankMap: Record<string, string> = {
    'ACE': 'A',
    'TWO': '2',
    'THREE': '3',
    'FOUR': '4',
    'FIVE': '5',
    'SIX': '6',
    'SEVEN': '7',
    'EIGHT': '8',
    'NINE': '9',
    'TEN': '10',
    'JACK': 'J',
    'QUEEN': 'Q',
    'KING': 'K',
    // 适配可能存在的简化输入
    'A': 'A',
    '2': '2',
    '3': '3',
    '4': '4',
    '5': '5',
    '6': '6',
    '7': '7',
    '8': '8',
    '9': '9',
    '10': '10',
    'J': 'J',
    'Q': 'Q',
    'K': 'K'
  };
  
  if (typeof rank.value === 'string' && rankMap[rank.value]) {
    return rankMap[rank.value];
  }
  
  return rank.value;
});

const suitClass = computed(() => {
  const suit = suitSymbol.value;
  if (suit === '♥' || suit === '♦' || suit === 'HEARTS' || suit === 'DIAMONDS') {
    return 'red';
  }
  return 'black';
});

const cardSizeClass = computed(() => {
  return `card-${props.size}`;
});
</script>

<style scoped>
.poker-card {
  position: relative;
  width: 60px;
  height: 90px;
  border-radius: 5px;
  background-color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  margin: 0 5px;
  transition: all 0.3s ease;
  cursor: pointer;
  user-select: none;
  display: inline-block;
  font-family: 'Arial', sans-serif;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background-image: radial-gradient(circle at 50% 50%, rgba(240, 240, 240, 0.8) 0%, rgba(255, 255, 255, 1) 70%);
}

/* 添加不同尺寸的卡片 */
.card-small {
  width: 45px;
  height: 67px;
  margin: 0 3px;
  font-size: 0.8em;
}

.card-mini {
  width: 30px;
  height: 45px;
  margin: 0 2px;
  font-size: 0.7em;
  border-radius: 3px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.card-mini .card-corner {
  padding: 2px;
}

.card-mini.selected {
  transform: translateY(-8px);
  border-width: 1.5px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

.poker-card:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 40%;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0));
  z-index: 1;
  pointer-events: none;
}

.poker-card.selected {
  transform: translateY(-15px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  border: 2px solid #409EFF;
}

.card-back {
  background: linear-gradient(135deg, #3498db, #2980b9);
  background-image: repeating-linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.1) 0px,
    rgba(255, 255, 255, 0.1) 5px,
    transparent 5px,
    transparent 10px
  );
}

.card-corner {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1;
  z-index: 2;
  padding: 4px;
  border-radius: 2px;
}

.top-left {
  top: 2px;
  left: 2px;
}

.bottom-right {
  bottom: 3px;
  right: 3px;
  transform: rotate(180deg);
}

.card-rank {
  font-size: 22px;
  font-weight: bold;
  line-height: 1.1;
}

.card-suit {
  font-size: 22px;
  line-height: 1.1;
  margin-top: 1px;
}

/* 不同尺寸卡片的角标大小 */
.card-small .card-rank {
  font-size: 18px;
}

.card-small .card-suit {
  font-size: 18px;
}

.card-mini .card-rank {
  font-size: 14px;
  font-weight: bold;
}

.card-mini .card-suit {
  font-size: 14px;
}

.red {
  color: #e74c3c;
}

.black {
  color: #2c3e50;
}
</style> 